﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Data;
using System.Data.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EffiProzDB;
using EffiProzDB.Lib;
using EffiProzDB.Persist;
using System.Data.EffiProzDB;

namespace EffiProzDBTest.SelfTest
{
    [TestClass]
    public class TestServer 
    {
        string testScriptPath = "../../../EffiProzDBTest/SelfTest/Scripts/";
        string serverDataPath = "../../../EffiProzDBTest/Data/Server/";
        string serverPath = @"C:\Users\irantha\Documents\Visual Studio 10\Projects\New folder\EffiProz\EffiProzDBServer\bin\Debug\EffiProzDBServer.exe";
        Process server;

        public void cleanupData()
        {
            string[] files = Directory.GetFiles(serverDataPath);
            foreach (var file in files)
                File.Delete(file);
        }

        public void StartServer()
        {
            server = new Process();
            server.StartInfo.FileName = serverPath;
            server.StartInfo.WorkingDirectory = Path.GetDirectoryName(serverPath);
            server.Start();
            Thread.Sleep(5000);
        }

        public void StartWebServer()
        {
            server = new Process();
            server.StartInfo.FileName = serverPath;
            server.StartInfo.WorkingDirectory = Path.GetDirectoryName(serverPath);
            server.StartInfo.Arguments = "HTTP";
            server.Start();
            Thread.Sleep(5000);
        }

        public void StopServer()
        {
            try
            {
                server.Kill();
            }
            catch (Exception)
            {
            }
        }

        public TestServer()         {
           
        }

        [TestMethod]
        public void ADONetBasicServer()
        {
            lock (this)
            {
                cleanupData();
                StartServer();

                string connString = "Type=Server ; server=localhost; port=9001; Data Source=testdb; User=sa; Password=;";
                string sql = "CREATE TABLE TEST(ID INT, NAME VARCHAR);";
                using (EffiProzDBConnection conn = new EffiProzDBConnection(connString))
                {
                    DbCommand command = conn.CreateCommand();
                    command.CommandText = sql;

                    conn.Open();
                    int count = command.ExecuteNonQuery();
                    command.CommandText = "INSERT INTO TEST(ID , NAME ) VALUES(1,'irantha'); INSERT INTO TEST(ID , NAME ) VALUES(NULL,'nuwan');";
                    count = command.ExecuteNonQuery();
                    Assert.AreEqual(count, 1);

                    command.CommandText = "SELECT * FROM TEST;";
                    DbDataReader reader = command.ExecuteReader();
                    System.Diagnostics.Debug.WriteLine(reader.FieldCount);
                    while (reader.Read())
                    {
                        if (reader.IsDBNull(0))
                            System.Diagnostics.Debug.WriteLine("ID= " + " Name=" + reader.GetString(1));
                        else
                            System.Diagnostics.Debug.WriteLine("ID= " + reader.GetInt32(0) + " Name=" + reader.GetString(1));
                    }

                    command = conn.CreateCommand();
                    command.CommandText = "Call Sqrt(25);";
                    double sqrt = (double)command.ExecuteScalar();
                    Assert.AreEqual(5, sqrt);
                }

                StopServer();
            }
        }

        [TestMethod]
        public void WebServerTestSelf()
        {
            lock (this)
            {
                cleanupData();
                StartWebServer();

                SessionInterface sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
                string scriptPath = testScriptPath + "TestSelf.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();

                StopServer();
            }
        }


        [TestMethod]
        public void WebServerTestSelfAlterTable()
        {
            lock (this)
            {
                cleanupData();
                StartWebServer();

                SessionInterface sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
                string scriptPath = testScriptPath + "TestSelfAlterTable.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();

                StopServer();
            }
        }

        [TestMethod]
        public void WebServerTestSelfSchemaPersistA()
        {
            lock (this)
            {
                cleanupData();
                StartWebServer();

                SessionInterface sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
                string scriptPath = testScriptPath + "TestSelfSchemaPersistA1.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();
                Thread.Sleep(5000);

                StopServer();

                Thread.Sleep(1000);

                StartWebServer();

                sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
                scriptPath = testScriptPath + "TestSelfSchemaPersistA2.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();


                StopServer();

            }
        }


        [TestMethod]
        public void ServerTestSelf()
        {
            lock (this)
            {
                cleanupData();
                StartServer();  

                SessionInterface sessionProxy = new HSQLClientConnection("localhost", 9001, "", "testdb", false, "sa", "");
                string scriptPath = testScriptPath + "TestSelf.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();

                StopServer();
            }
        }

        [TestMethod]
        public void ServerTestSelfAlterTable()
        {
            lock (this)
            {
                cleanupData();
                StartServer();  

                SessionInterface sessionProxy = new HSQLClientConnection("localhost", 9001, "", "testdb", false, "sa", "");
                string scriptPath = testScriptPath + "TestSelfAlterTable.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();

                StopServer();
            }
        }

        [TestMethod]
        public void ServerTestSelfSchemaPersistA()
        {
            lock (this)
            {
                cleanupData();
                StartServer();

                SessionInterface sessionProxy = new HSQLClientConnection("localhost", 9001, "", "testdb", false, "sa", "");
                string scriptPath = testScriptPath + "TestSelfSchemaPersistA1.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();
                Thread.Sleep(5000);

                StopServer();

                Thread.Sleep(1000);

                StartServer();

                sessionProxy = new HSQLClientConnection("localhost", 9001, "", "testdb", false, "sa", "");
                scriptPath = testScriptPath + "TestSelfSchemaPersistA2.txt";
                TestUtil.testScript(sessionProxy, scriptPath);
                sessionProxy.close();
             

                StopServer();

            }
        }
    }
}
